package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.bean.MessageBean;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.MessageInteactor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/22.
 */
public class MessageInteractorImp extends NetWorkImp implements MessageInteactor {

    RequestListener<List<MessageBean>> listener;

    public MessageInteractorImp(Context c, RequestListener<List<MessageBean>> listener) {
        super(c);
        this.listener = listener;
    }

    @Override
    public void getMessage(String userId, int currentPage) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
//        params.put("sessionId",userId);
//        params.put("type", type);
        bean.setRequestParams(params);
        bean.setRequestContentCurrentPage(currentPage);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.GET_MESSAGE);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_MESSAGE, null, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }


    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {

            if (tag.toString().equals(MethodType.GET_MESSAGE)) {
                JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                List<MessageBean> bean = JSON.parseArray(obj.getJSONArray("message").toJSONString(), MessageBean.class);
                if(bean!=null)
                    listener.Success(tag+"",bean);
                else
                    listener.Error(tag + "", c.getString(R.string.system_error));
            }
        }else
            listener.Error(tag + "", c.getString(R.string.system_error));
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("", error);
    }

}
